<?php
class ContentService extends My_Service
{   
	public function buildQueryData($orderBy = "", $orderDirection="",
	$key = "", $field = "", $params = null)
	{
		if(empty($orderBy))
		{
			$orderBy = "ordering";
		}
		return parent::buildQueryData($orderBy,$orderDirection,$key,$field,$params);
	}
	
    public function check()
    {
		//validate Title
		$titleVald = new Zend_Validate_StringLength(3, 255);
		if(!$titleVald->isValid($_POST['title']))
		{
			$this->_errorList["title"] = My_Message::get("S002|Title|10|255");
			return false;
		}
		
		//validate Summary
		$summaryVald = new Zend_Validate_StringLength(3);
		if(!$summaryVald->isValid($_POST['summary']))
		{
			$this->_errorList["summary"] = My_Message::get("S003|Summary|10");
			return false;
		}
		
		//validate category
		if(empty($_POST['categoryId']))
		{
			$this->_errorList["category"] = My_Message::get("S001|Category");
		}
		
		//validate body
		$bodyVald = new Zend_Validate_StringLength(10);
		if(!$bodyVald->isValid($_POST['body']))
		{
			$this->_errorList["body"] = My_Message::get("S003|Body|50");
		}
		
		//validate image
		if(!empty($_FILES['imageURL']['name']))
		{
			$adapter = new Zend_File_Transfer_Adapter_Http();

			$adapter->addValidator('IsImage', false);
			if(!$adapter->isValid())
			{
				$this->_errorList["imageURL"] = My_Message::get("S004");
				return false;
			}

			$uploadDir = ARTICLE_IMG_UPLOAD_PATH;
			if(!is_readable($uploadDir))
			{
				$this->_errorList["imageURL"] = My_Message::get("S005|Image");
				return false;
			}
			
			$rand = date("Ymdhis").rand()."_";
			$fileName = $rand.$_FILES ['imageURL']['name'];
			$res = move_uploaded_file($_FILES ['imageURL']['tmp_name'],$uploadDir."/".$fileName);
			if (! $res) {
				$this->_errorList["imageURL"] = My_Message::get("S005|Image");
				return false;
			}
			$_POST['imageURL']['name'] = $fileName;
		}
		
		if(empty($this->_errorList) == false)
		{
			return false;
		}
		return true;
    }
    
	public function bind()
	{
		$content = new ContentDTO( );
		
		if(!empty($_POST['contentId']))//case edit
		{
			$content = $this->load($_POST['contentId']);
		}
		
		$content->title = $_POST['title'];
		$content->summary = $_POST['summary'];
		$content->body = $_POST['body'];
		$content->status = $_POST['status'];
		$content->categoryId = $_POST['categoryId'];
		if(empty($_POST['contentId']))//case insert
		{
			$content->creationTime = date ( "Y-m-d h:i:s" );
			$content->ordering = 1;
			$content->createdBy = 2;
		}
		
		if (! empty ( $_POST ['imageURL']['name'] )) 
		{
			if(!empty($_POST['contentId']))//case edit, remove old images
			{
				$uploadDir = ARTICLE_IMG_UPLOAD_PATH;
				$imgFile = $uploadDir.$content->imageURL;
				$thumbFile = $uploadDir.$content->thumbnailURL;
				@unlink($imgFile);
				@unlink($thumbFile);	
			}
			$content->imageURL = basename ( $_POST ['imageURL'] ['name'] );
			$uploadDir = ARTICLE_IMG_UPLOAD_PATH;
			$content->thumbnailURL = My_Utility::createThumb ( $uploadDir . $content->imageURL, 99, 99 );
			
		}
		
		return $content;
	}
    
    public function store()
    {
    	//check input data
    	if(!$this->check())
    	{
			return false;
    	}
    	
    	//bind data
    	$content = $this->bind();
    	
    	//save data
    	$this->save($content);
    	return true;
    }
    
	public function remove($contentId)
	{
		$content = $this->load($contentId);
		$uploadDir = ARTICLE_IMG_UPLOAD_PATH;
		$imgFile = $uploadDir.$content->imageURL;
		$thumbFile = $uploadDir.$content->thumbnailURL;
		@unlink($imgFile);
		@unlink($thumbFile);
		parent::remove($contentId);
	}
	
	public function loadCategoryOptions()
	{
		$categoryDAO = new CategoryDAO();
		
		$categoryIdList = $categoryDAO->loadCol('categoryId');
		$categoryNameList = $categoryDAO->loadCol('categoryName');
		
		array_unshift($categoryIdList,0);
		array_unshift($categoryNameList,"- Select Category -");
		
		return array_combine($categoryIdList,$categoryNameList);
	}
    
	public function getPublish($catId)
	{
		return $this->getDAO()->getPublish($catId);
	}
	
	public function getLibrary()
	{
		$langCode = My_Translate::getLanguageCode();
		if($langCode == "vn")
		{
			$catId = 20;
		}
		else
		{
			$catId = 29;
		}
		return $this->getPublish($catId);
	}
	
	public function getEvents()
	{
		$langCode = My_Translate::getLanguageCode();
		if($langCode == "vn")
		{
			$catId = 30;
		}
		else
		{
			$catId = 31;
		}
		return $this->getPublish($catId);
	}
	
	public function getFirstEvent()
	{
		$langCode = My_Translate::getLanguageCode();
		if($langCode == "vn")
		{
			$catId = 30;
		}
		else
		{
			$catId = 31;
		}
		$select = $this->getDAO()->getFirstPublish($catId);
		//echo $select->__toString();exit;
		return $select->query()->fetchAll();
	}
	
 	public function getFeedback()
    {
    	$langCode = My_Translate::getLanguageCode();
		if($langCode == "vn")
		{
			$id = 38;
		}
		else
		{
			$id = 41;
		}
		return $this->load($id);
    }
    
    public function getContentWithType($type)
    {
    	$langCode = My_Translate::getLanguageCode();
		if($langCode == "vn")
		{
			if($type == "news")
			{
				$id = 22;
			}
			if($type == "software")
			{
				$id = 24;
			}
			if($type == "nhansu")
			{
				$id = 31;
			}
			if($type == "hoatdong")
			{
				$id = 32;
			}
			if($type == "kinhdoanh")
			{
				$id = 39;
			}
			if($type == "tuyendung")
			{
				$id = 46;
			}
		}
		else
		{
			if($type == "news")
			{
				$id = 26;
			}
			if($type == "software")
			{
				$id = 40;
			}
			if($type == "nhansu")
			{
				$id = 30;
			}
			if($type == "hoatdong")
			{
				$id = 27;
			}
			if($type == "kinhdoanh")
			{
				$id = 42;
			}
			if($type == "tuyendung")
			{
				$id = 47;
			}
		}
		return $this->load($id);
    }
}
?>